package com.sz.query.service.impl;

import com.sz.query.dao.TransferDao;
import com.sz.query.entity.Transfer;
import com.sz.query.service.TransferService;
import com.sz.query.vo.PageVO;
import com.sz.query.vo.QueryVO;
import com.sz.query.vo.TransferVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class TransferServiceImpl implements TransferService {
    @Autowired
    TransferDao transferDao;

    public QueryVO getTransfer(TransferVO vo) {
       QueryVO queryVO = new QueryVO();

        vo.setStart((vo.getIndex()-1)*vo.getPageSize()+1);
        vo.setEnd(vo.getIndex()*vo.getPageSize());
        Integer pageCount = (transferDao.getConditionRows(vo)+vo.getPageSize()-1)/vo.getPageSize();
        List<Transfer> transfers = transferDao.getTransfers(vo);
       queryVO.setPageCount(pageCount);
       queryVO.setTransfers(transfers);
        return queryVO;
    }

    public Transfer getTransferByDealNo(String dealNo) {
        return transferDao.getTransferByDealNo(dealNo);
    }

    public QueryVO getTransferByName(PageVO vo) {
        QueryVO queryVO = new QueryVO();
        vo.setStart((vo.getIndex()-1)*vo.getPageSize()+1);
        vo.setEnd(vo.getIndex()*vo.getPageSize());
        Integer pageCount = (transferDao.getTransferByNameRows(vo)+vo.getPageSize()-1)/vo.getPageSize();
        List<Transfer> transfers = transferDao.getTransferByName(vo);
        queryVO.setTransfers(transfers);
        queryVO.setPageCount(pageCount);
        return queryVO;
    }

    public QueryVO getTransferByAccId(PageVO vo) {
       QueryVO queryVO = new QueryVO();
        vo.setStart((vo.getIndex()-1)*vo.getPageSize()+1);
        vo.setEnd(vo.getIndex()*vo.getPageSize());
        Integer pageCount =(transferDao.getTransferByAccIdRows(vo) +vo.getPageSize()-1)/vo.getPageSize();
        List<Transfer> transfers = transferDao.getTransferByAccId(vo);
        queryVO.setPageCount(pageCount);
        queryVO.setTransfers(transfers);
        return queryVO;
    }

    public List<Transfer> getTransferByTime(String date1,String date2 ,Long status) {
        return transferDao.getTransferByTime(date1,date2,status);
    }

    public Double getTransferByAccIN(String  accIn) {
        Double temp =0.0;
        List<Double> in = transferDao.getTransferByNowIn(accIn);
        if(in.isEmpty()){
            return  0.0;
        }else{
            for (Double value : in) {
                temp = temp +value;
            }
            return temp;
        }
    }

    public Double getTransferByAccOut(String accOut) {
        Double temp = 0.0;
        List<Double> out = transferDao.getTransferByNowOut(accOut);
        if(out.isEmpty()){
            return  0.0;
        }else{
            for (Double value : out) {
                temp = temp +value;
            }
            return temp;
        }
    }

    public QueryVO getTransferErr(PageVO vo) {
        QueryVO queryVO = new QueryVO();
        vo.setStart((vo.getIndex()-1)*vo.getPageSize()+1);
        vo.setEnd(vo.getIndex()*vo.getPageSize());
        Integer pageCount =(transferDao.getTransferErrCount(vo) +vo.getPageSize()-1)/vo.getPageSize();
        List<Transfer> transfers = transferDao.getTransferErr(vo);
        queryVO.setPageCount(pageCount);
        queryVO.setTransfers(transfers);
        return queryVO;
    }

}
